clc;
clearvars;
%% Load or Make Data
TempDataFile='Data\PerformanceDataUFS_NoB.mat';
load(TempDataFile,'Test','EstimatedValue','KNNNum','RNGState')

FitNum=size(Test.BestFittedW,2);
%% Extract Error MAE and STD
MAE=zeros(KNNNum+FitNum,1);
STD=zeros(KNNNum+FitNum,1);
R=zeros(KNNNum+FitNum,1);
for i=1:KNNNum
    Erorrs=EstimatedValue(:,i)-Test.Label;
    Erorrs(isnan(Erorrs))=[];
    AE = abs(Erorrs);
    MAE(i)=mean(AE);
    STD(i)=std(AE);
    R(i)=corr(EstimatedValue(:,i),Test.Label);
end
for i=KNNNum+1:KNNNum+FitNum
    Erorrs=Test.BestFittedW(:,i-KNNNum)-Test.Label;
    Erorrs(isnan(Erorrs))=[];
    AE = abs(Erorrs);
    MAE(i)=mean(AE);
    STD(i)=std(AE);
    R(i)=corr(Test.BestFittedW(:,i-KNNNum),Test.Label);
end
%% Plots Options
BasePlotFileName='Plots\Performance_Fig';
TextSize=14;
SavePlots=1;
MaxC=0.3;
Titles = {'\itk-nn (\wp)' ,...
          '\itk-nn (\wp_{sub1})','\itk-nn (\wp_{sub2})', ...
          'ML' ,'MAP'       };


YLabels= {'$\hat{w}_{knn}$' , '$\hat{w}_{knn 1}$', '$\hat{w}_{knn 2}$',...
          '$\hat{w}_{ML}$' ,'$\hat{w}_{MAP}$'      };


%% Extract Error Histograms
Bins=-1:0.1:1;
AbsBins=0:0.1:1;
% B=[-1,-0.9 -0.8 -0.7 -0.6 -0.5 -0.3 -0.15];
% Bins=[B,0,-B(end:-1:1)];
BinNum=length(Bins);
AbsBinNum=length(AbsBins);
Hist=zeros(BinNum,KNNNum+FitNum);
AbsHist=zeros(AbsBinNum,KNNNum+FitNum);
for i=1:KNNNum
    Erorrs=EstimatedValue(:,i)-Test.Label;
    Erorrs(isnan(Erorrs))=[];
    Hist(:,i)=hist(Erorrs,Bins);
    AbsHist(:,i)=hist(abs(Erorrs),AbsBins);
end
for i=KNNNum+1:KNNNum+FitNum
    Erorrs=Test.BestFittedW(:,i-KNNNum)-Test.Label;
    Erorrs(isnan(Erorrs))=[];
    Hist(:,i)=hist(Erorrs,Bins);
    AbsHist(:,i)=hist(abs(Erorrs),AbsBins);
end
% Hist(11,1:3)=Hist(11,1:3)-250;
% Hist(12,1:3)=Hist(12,1:3)-250;
% Hist(13,1:3)=Hist(13,1:3)-200;
% Hist(14,1:3)=Hist(14,1:3)-150;
% Hist(15,1:3)=Hist(15,1:3)-100;
% Hist(16,1:3)=Hist(16,1:3)-50;
% Hist(17,1:3)=Hist(17,1:3)-15;
%% Plot Histogram Bar of Errors
figure(61);clf
b=bar(Bins,Hist/Test.SNum);
b(1).FaceColor=[0,1,0];
b(2).FaceColor=[1,1,0];
b(3).FaceColor=[0,1,1];
b(4).FaceColor=[1,0,0];
b(5).FaceColor=[1,0,1];
set(gca,'FontSize',TextSize)
set(gca,'YScale','log')
xlim([-1.1,1.1])
xticks(-1:0.1:1);
xtickangle(90);
legend(Titles,'location','NorthEast')
xlabel('Error =$\hat{w}-w_{o}$','Interpreter','latex');
ylabel('Probability of Error','Interpreter','latex');
set(gcf,'Position',[50,50,1200,500]);
if SavePlots
FileName=[BasePlotFileName,'2.1_ErrorsHistNew_Fig8'];
saveas(gcf,[FileName,'.jpg']);
saveas(gcf,[FileName,'.emf']);
end
%% Plot Histogram Bar of abs(Errors)
figure(62);clf
b=bar(AbsBins,AbsHist/Test.SNum);
b(1).FaceColor=[0,1,0];
b(2).FaceColor=[1,1,0];
b(3).FaceColor=[0,1,1];
b(4).FaceColor=[1,0,0];
b(5).FaceColor=[1,0,1];
set(gca,'FontSize',TextSize)
set(gca,'YScale','log')
xlim([-0.1,1.1])
xticks(0:0.1:1);
xtickangle(90);
legend(Titles,'location','NorthEast')
xlabel('Error =$\|hat{w}-w_{o}|$','Interpreter','latex');
ylabel('Probability of Error','Interpreter','latex');
set(gcf,'Position',[50,50,1200,500]);
if SavePlots
FileName=[BasePlotFileName,'2.2_AbsErrorsHistNew_Fig8'];
saveas(gcf,[FileName,'.jpg']);
saveas(gcf,[FileName,'.emf']);
end
%% Plot Probability of Errors
figure(63);clf
Hist(Hist==0)=1e-7;
plot(Bins,Hist/Test.SNum,'LineWidth',3)

set(gca,'FontSize',TextSize)
set(gca,'YScale','log')
xlim([-1.1,1.1])
ylim([5e-5,1])
% xticks(0:0.2:1);
% xtickangle(90);
legend(Titles,'location','NorthEast')
xlabel('Error =$\hat{w}-w_{o}$','Interpreter','latex');
ylabel('Probability of Error','Interpreter','latex');
set(gcf,'Position',[50,50,1200,500]);
if SavePlots
FileName=[BasePlotFileName,'2.3_AbsErrorsPlotNew_Fig8'];
saveas(gcf,[FileName,'.jpg']);
saveas(gcf,[FileName,'.emf']);
end
%% Plot Probability of Abs Errors
figure(64);clf
AbsHist(AbsHist==0)=1e-7;
plot(AbsBins,AbsHist/Test.SNum,'LineWidth',3)

set(gca,'FontSize',TextSize)
set(gca,'YScale','log')
xlim([-.1,1.1])
ylim([5e-5,1])
% xticks(0:0.2:1);
% xtickangle(90);
legend(Titles,'location','NorthEast')
xlabel('Error =$|\hat{w}-w_{o}|$','Interpreter','latex');
ylabel('Probability of Error','Interpreter','latex');
set(gcf,'Position',[50,50,1200,500]);
if SavePlots
FileName=[BasePlotFileName,'2.4_AbsErrorsPlotNew_Fig8'];
saveas(gcf,[FileName,'.jpg']);
saveas(gcf,[FileName,'.emf']);
end